using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Net;

namespace net.notdot.Attercop
{
    /// <summary>
    /// Encapsulates information about a known peer.
    /// </summary>
    public class Peer
    {
        /// <summary>
        /// An RSACryptoServiceProvider that can be used to verify messages signed by this user
        /// </summary>
        private readonly RSACryptoServiceProvider m_PublicKey;

        public RSACryptoServiceProvider PublicKey
        {
            get { return m_PublicKey; }
        } 


        /// <summary>
        /// The user's unicast IP address and port on which they can receive UDP messages.
        /// </summary>
        private readonly IPEndPoint m_Address;
        public IPEndPoint Address
        {
            get { return m_Address; }
        }

        /// <summary>
        /// The user's nickname.
        /// </summary>
        private string m_Nick;
        public string Nick
        {
            get { return m_Nick; }
            set { m_Nick = value; }
        }

        /// <summary>
        /// The total amount this user has shared, in bytes.
        /// </summary>
        private long m_TotalShared;
        public long TotalShared
        {
            get { return m_TotalShared; }
            set { m_TotalShared = value; }
        }

        internal DateTime m_LastSeen;
        /// <summary>
        /// The time at which a message from this peer was last received.
        /// </summary>
        public DateTime LastSeen
        {
            get
            {
                return m_LastSeen;
            }
        }

        public Peer(IPEndPoint address, RSACryptoServiceProvider pubkey, string nick, long totalShared)
        {
            this.m_Address = address;
            this.m_PublicKey = pubkey;
            this.Nick = nick;
            this.TotalShared = totalShared;
        }

        /// <summary>
        /// The sequence number of the last message removed from this user's queue of recent messages.
        /// </summary>
        /// <remarks>
        /// Any packets with a sequence number lower than this value will be discarded.
        /// </remarks>
        internal long MinSequenceNumber = 0;

        internal bool m_Introduced = false;

        /// <summary>
        /// Indicates if we have completed an introduction with this peer or not.
        /// </summary>
        /// <remarks>
        /// All new peers are not introduced, until they have been sent a unicast PingMessage, and a valid
        /// signed response has been received from them.
        /// 
        /// Most messages from non-introduced peers should be ignored.
        /// </remarks>
        public bool Introduced {
            get {
                return m_Introduced;
            }
        }

        internal bool m_Offline = false;

        /// <summary>
        /// If true, this peer has timed out (gone offline, presumably), but
        /// has not yet been removed from the list of peers, in case it comes back.
        /// </summary>
        public bool Offline
        {
            get
            {
                return m_Offline;
            }
        }
    }
}
